package com.module.aichat.ui.chatMain.provider

import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.airbnb.lottie.LottieAnimationView
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.utls.TimeUtils
import com.common.base.app.extras.loadId
import com.google.android.material.imageview.ShapeableImageView
import com.module.aichat.R
import com.module.aichat.data.bean.MsgDirection
import com.module.aichat.db.table.DBMessage

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/06/25
 *
 * 描述：ReceiveChatMessage
 *
 * 修订历史：
 *
 */
abstract class BaseMessageItemProvider : BaseItemProvider<DBMessage>() {

    override var layoutId: Int = R.layout.aichat_single_chat_base_item

    protected var attachView: View? = null

    override fun convert(helper: BaseViewHolder, item: DBMessage) {
        item.let {
            helper.setGone(R.id.rcTime, !shouldShowTimeForMessage(helper.layoutPosition, it.sendTime))

            helper.setText(
                R.id.rcTime,
                TimeUtils.getMonthDayHourMinute(it.sendTime)
            )
            //isMeSend == 1 表示消息是我发送的  2表示为收到的消息
            if (it.msgDirection == MsgDirection.SEND) {
                helper.setGone(R.id.rcLeftPortrait, true)
//                helper.setVisible(R.id.rcRightPortrait, true)
                //加载右侧头像
                helper.getView<ShapeableImageView>(R.id.rcRightPortrait).loadId(R.drawable.common_user_def_head_icon, 100f)
            } else {
                helper.setVisible(R.id.rcLeftPortrait, true)
//                helper.setGone(R.id.rcRightPortrait, true)
                //加载左侧头像
                helper.getView<ShapeableImageView>(R.id.rcLeftPortrait).loadId(R.mipmap.ic_logo_launcher, 100f)
            }

            val contentContainer = helper.getView<FrameLayout>(R.id.rcContent)
            if ((contentContainer.childCount <= 0)) {
                contentContainer.removeAllViews()
                LayoutInflater.from(contentContainer.context)
                    .inflate(getContentLayoutId(), contentContainer)
            }

            //如果是群聊，且消息是别人发的，才显示发送者名称
            if (it.msgDirection == MsgDirection.RECEIVE) {
                //是否需要显示消息的发送者名称
                helper.setGone(R.id.rcTitle, false)
                helper.setText(R.id.rcTitle, "小慧助手")
            } else {
                helper.setGone(R.id.rcTitle, true)
            }

            when(it.sendStatus){//消息状态0等待中1完成2错误
                1-> {
                    helper.setVisible(R.id.rcContent, true)
                    helper.setGone(R.id.totalLoading, true)
                    helper.setGone(R.id.warningTotal, true)
                    helper.getView<LottieAnimationView>(R.id.rcProgress).pauseAnimation()
                }
                2-> {
                    helper.setGone(R.id.rcContent, true)
                    helper.setGone(R.id.totalLoading, true)
                    helper.setVisible(R.id.warningTotal, true)
                    helper.getView<LottieAnimationView>(R.id.rcProgress).pauseAnimation()
                }
                else->{
                    helper.setGone(R.id.rcContent, true)
                    helper.setVisible(R.id.totalLoading, true)
                    helper.setGone(R.id.warningTotal, true)
                    helper.getView<LottieAnimationView>(R.id.rcProgress).playAnimation()
                }
            }
            //渲染具体的消息Item
            convertContent(helper, it)
        }
    }

    abstract fun getContentLayoutId(): Int

    abstract fun convertContent(helper: BaseViewHolder, message: DBMessage?)


    /**
     * 设置消息的长按事件
     */
    private fun setOnMessageLongClick(view: View?, disPlayMessage: DBMessage) {
        view?.setOnLongClickListener {
//            (getAdapter() as SingleChatAdapter).onMessageLongClick?.invoke(it, disPlayMessage)
            true
        }
    }


    /**
     * 判断是否需要显示时间
     * 如果当前消息与上一条消息的时间差不超过1分钟，则不显示时间
     */
    private fun shouldShowTimeForMessage(position: Int, currentSendTime: Long): Boolean {
        // 如果是第一条消息，总是显示时间
        if (position == 0) {
            return true
        }

        // 获取上一条消息
        val adapter = getAdapter()
        if (adapter != null && position > 0 && position - 1 < adapter.data.size) {
            val previousMessage = adapter.data[position - 1]
            val timeDifference = currentSendTime - previousMessage.sendTime
            // 如果时间差小于等于1分钟(60000毫秒)，不显示时间
            return timeDifference > 60000
        }

        // 默认显示时间
        return true
    }
}